package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IAfficheService;
import com.younion.model.MessageCenterResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.AfficheVO;

public class MessageCenterConsumer extends AbstractRequestConsumer {
	private IAfficheService afficheService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getUpdateInfo(request, response);
	}

	public String getUpdateInfo(HttpServletRequest request,
			HttpServletResponse response) {
		List<MessageCenterResult> messageCenterResultList = new ArrayList<MessageCenterResult>();
		List<AfficheVO> afficheVOs = null;
		ResultObject resultObject = new ResultObject();
		try {
			afficheVOs = afficheService.selectApiAfficheByMap(null);
			if (afficheVOs != null && afficheVOs.size() > 0) {
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
				for (AfficheVO afficheVO : afficheVOs) {
					MessageCenterResult messageCenterResult = new MessageCenterResult();
					messageCenterResult.setId(afficheVO.getId().toString());
					messageCenterResult.setTitle(afficheVO.getTitle());
					messageCenterResult.setContent(afficheVO.getContent());
					messageCenterResult.setIntroduction(afficheVO
							.getTitleShow());
					messageCenterResult.setTime(sf.format(afficheVO.getIssueTime()));
					messageCenterResultList.add(messageCenterResult);
				}
			}
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		resultObject.setResult(messageCenterResultList);
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setAfficheService(IAfficheService afficheService) {
		this.afficheService = afficheService;
	}

}
